package org.jsola.hr.query;

import io.swagger.annotations.ApiParam;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.hibernate.validator.constraints.Length;
import org.jsola.core.query.BaseQuery;
import org.jsola.hr.constant.AttendStatus;
import org.jsola.orm.filter.Order;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.validation.constraints.Max;
import javax.validation.constraints.Min;

import static org.jsola.core.example.BaseExample.E_CREATE_USER_ID;
import static org.jsola.core.example.BaseExample.E_MODIFIED_USER_ID;
import static org.jsola.core.example.BaseExample.N_CREATE_USER_ID;
import static org.jsola.core.example.BaseExample.N_MODIFIED_USER_ID;
import static org.jsola.hr.example.AttendanceDedExample.E_AMOUNT_ABSENTEEISM;
import static org.jsola.hr.example.AttendanceDedExample.E_AMOUNT_MINUTE;
import static org.jsola.hr.example.AttendanceDedExample.E_COMPANY_ID;
import static org.jsola.hr.example.AttendanceDedExample.E_DED_TYPE;
import static org.jsola.hr.example.AttendanceDedExample.E_GMT_CREATE_END;
import static org.jsola.hr.example.AttendanceDedExample.E_GMT_CREATE_START;
import static org.jsola.hr.example.AttendanceDedExample.E_GMT_MODIFIED_END;
import static org.jsola.hr.example.AttendanceDedExample.E_GMT_MODIFIED_START;
import static org.jsola.hr.example.AttendanceDedExample.E_NAME;
import static org.jsola.hr.example.AttendanceDedExample.E_OVERALL_RANGE;
import static org.jsola.hr.example.AttendanceDedExample.E_PROGRAMME_ID;
import static org.jsola.hr.example.AttendanceDedExample.E_STATUS;
import static org.jsola.hr.example.AttendanceDedExample.E_TYPE;
import static org.jsola.hr.example.AttendanceDedExample.N_AMOUNT_ABSENTEEISM;
import static org.jsola.hr.example.AttendanceDedExample.N_AMOUNT_MINUTE;
import static org.jsola.hr.example.AttendanceDedExample.N_COMPANY_ID;
import static org.jsola.hr.example.AttendanceDedExample.N_DED_TYPE;
import static org.jsola.hr.example.AttendanceDedExample.N_GMT_CREATE_END;
import static org.jsola.hr.example.AttendanceDedExample.N_GMT_CREATE_START;
import static org.jsola.hr.example.AttendanceDedExample.N_GMT_MODIFIED_END;
import static org.jsola.hr.example.AttendanceDedExample.N_GMT_MODIFIED_START;
import static org.jsola.hr.example.AttendanceDedExample.N_NAME;
import static org.jsola.hr.example.AttendanceDedExample.N_OVERALL_RANGE;
import static org.jsola.hr.example.AttendanceDedExample.N_PROGRAMME_ID;
import static org.jsola.hr.example.AttendanceDedExample.N_STATUS;
import static org.jsola.hr.example.AttendanceDedExample.N_TYPE;

/**
 * @author zhr
 */
@EqualsAndHashCode(callSuper = true)
@Data
public class AttendanceDedQuery extends BaseQuery {

    private static final long serialVersionUID = 1L;

    @ApiParam(value = N_PROGRAMME_ID, example = E_PROGRAMME_ID)
    @Length(max = 32)
    private String programmeId;

    @ApiParam(value = N_DED_TYPE, example = E_DED_TYPE, allowableValues = AttendStatus.VALUES_STR)
    @Length(max = 4)
    private String dedType;

    @ApiParam(value = N_NAME, example = E_NAME)
    @Length(max = 255)
    private String name;

    @ApiParam(value = N_TYPE, example = E_TYPE)
    private Integer type;

    @ApiParam(value = N_AMOUNT_MINUTE, example = E_AMOUNT_MINUTE)
    private Integer amountMinute;

    @ApiParam(value = N_AMOUNT_ABSENTEEISM, example = E_AMOUNT_ABSENTEEISM)
    private Integer amountAbsenteeism;

    @ApiParam(value = N_OVERALL_RANGE, example = E_OVERALL_RANGE)
    @Length(max = 255)
    private String overallRange;

    @ApiParam(value = N_STATUS, example = E_STATUS)
    private Boolean status;

    @ApiParam(value = N_COMPANY_ID, example = E_COMPANY_ID)
    @Length(max = 32)
    private String companyId;

    /**
     * 创建时间查询范围
     */
    @ApiParam(value = N_GMT_CREATE_START, example = E_GMT_CREATE_START)
    private Date gmtCreateStart;
    @ApiParam(value = N_GMT_CREATE_END, example = E_GMT_CREATE_END)
    private Date gmtCreateEnd;


    @ApiParam(value = N_CREATE_USER_ID, example = E_CREATE_USER_ID)
    @Length(max = 32)
    private String createUserId;

    /**
     * 修改时间查询范围
     */
    @ApiParam(value = N_GMT_MODIFIED_START, example = E_GMT_MODIFIED_START)
    private Date gmtModifiedStart;
    @ApiParam(value = N_GMT_MODIFIED_END, example = E_GMT_MODIFIED_END)
    private Date gmtModifiedEnd;


    @ApiParam(value = N_MODIFIED_USER_ID, example = E_MODIFIED_USER_ID)
    @Length(max = 32)
    private String modifiedUserId;



    @ApiParam(value = "排序字段", allowMultiple=true, type="Order" ,example = "\"orders\": [{\"property\": \"userName\",\"desc\": false}]")
    private List<Order> orders;

    /**
     * 每页的条数
     */
    @ApiParam(value = "分页参数，每页的条数", example = "20")
    @Max(value = 1000, message = "pageSize不能大于1000")
    private Integer pageSize = 20;

    /**
     * 当前第几页
     */
    @ApiParam(value = "分页参数，第几页", example = "1")
    @Min(value = 1, message = "pageNo不能小于1")
    private Integer pageNo = 1;



    /**
     * 添加排序
     * @param propertyName 属性名
     * @param desc 是否降序
     * @return this
     */
    public AttendanceDedQuery addOrder(String propertyName, boolean desc) {
        if (CollectionUtils.isEmpty(this.orders)) {
            this.orders = new ArrayList<>();
        }
        this.orders.add(new Order(propertyName,desc));
        return this;
    }

}

